import { system, world } from "@minecraft/server";

import { showMainGui } from './ui/MainUi'

import { piston_intercept, operational_interception } from "./event/block"
import { run_commands } from "./event/player"
import { set_tick_value } from "./utils/Game"

import {
    enforcePlotProtection,
} from "./core/land"

// system.runTimeout(() => {
//     world.setDynamicProperty("land_list", "")
// })

// 插件主循环
// 每 5 gametick 执行一次
system.runInterval(() => {
    enforcePlotProtection();
    set_tick_value(5)
}, 5);

// 活塞推动后事件
world.afterEvents.pistonActivate.subscribe(event => {
    piston_intercept(event)
});

// 玩家发送消息前事件
world.beforeEvents.chatSend.subscribe((event) => {
    if (event.message[0] === "!") run_commands(event);
});

// 玩家发送表情后事件
world.afterEvents.playerEmote.subscribe(event => {
    if (event.personaPieceId == "17428c4c-3813-4ea1-b3a9-d6a32f83afca") showMainGui(event.player)
});

// 玩家使用物品后事件
world.afterEvents.itemUse.subscribe((event) => {
    var item = event.itemStack
    var player = event.source

    if (item.typeId == 'maskviva:menu') showMainGui(player)
});


// 玩家破坏方块事件
world.beforeEvents.playerBreakBlock.subscribe((event) => {
    const player = event.player
    const { x, z } = event.block
    operational_interception(event, player, x, z)
});

// 玩家放置方块事件
world.beforeEvents.playerPlaceBlock.subscribe((event) => {
    const player = event.player
    const { x, z } = event.block
    operational_interception(event, player, x, z)
});

// 玩家放置方块事件
world.beforeEvents.playerInteractWithBlock.subscribe((event) => {
    const player = event.player
    const { x, z } = event.block
    operational_interception(event, player, x, z)
});